package com.example.aopdemo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-26
 * Time: 18:42
 */
@Slf4j
//@Aspect
@Component
public class AspectDemo {
    @Pointcut("execution(* com.example.aopdemo.controller.*.*(..))")
    public void pt(){};

    @Before("pt()")
    public void doBefore() {
        log.info("AspectDemo执行doBefore....");
    }


    @After("pt()")
    public void doAfter() {
        log.info("AspectDemo执行doAfter....");
    }

    @AfterReturning("pt()")
    public void doAfterReturning() {
        log.info("AspectDemo执行AfterReturning....");
    }


    @AfterThrowing("pt()")
    public void doAfterThrowing() {
        log.info("AspectDemo执行doAfterThrowing....");
    }

    @Around("pt()")
    public Object timeRecord(ProceedingJoinPoint pjt) {
        log.info("TimeRecordAspect执行前...");
        long start = System.currentTimeMillis();
        Object o = null;
        try {
            o = pjt.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        log.info("TimeRecordAspect执行后...");
        log.info(pjt.getSignature() + "耗时" + (System.currentTimeMillis() - start) + "ms");
        return o;
    }

}
